const db = require('../../db/index')
//查系列
async function querySeries () {
  try {
    let result = await db.query(`select cycle_name from package_cycle`)
    if (result.length < 1) {
      throw new Error('系列不存在,查询失败')
    }
    result = result.map((item) => {
      return item.cycle_name
    })
    return result
  } catch (error) {
    throw error
  }
}
//查所有卡包
async function queryPackage () {
  try {
    let result = await db.query(`select detail_name from package_detail`)
    if (result.length < 1) {
      throw new Error('卡包不存在,查询失败')
    }
    result = result.map((item) => {
      return item.detail_name
    })
    return result
  } catch (error) {
    throw error
  }
}
//查所有类型
async function queryType () {
  try {
    let result = await db.query(`select distinct type_one, type_two from card`)
    if (result.length < 1) {
      throw new Error('类型不存在,查询失败')
    }
    _result = {
      typeOne: [],
      typeTwo: []
    }
    result.map((item) => {
      _result.typeOne.push(item.type_one)
      if (_result.typeTwo.indexOf(item.type_two) === -1) {
        _result.typeTwo.push(item.type_two)
      }
    })
    return _result
  } catch (error) {
    console.log(error.message);
    throw error
  }
}
//查所有罕贵
async function queryRare () {
  try {
    let result = await db.query(`select rare  from rare`)
    if (result.length < 1) {
      throw new Error('罕贵查询异常,查询失败')
    }
    result = result.map((item) => {
      return item.rare
    })
    return result
  } catch (error) {
    console.log(error.message);
    throw error
  }
}
//根据系列查卡包
async function queryPackBySeries (series) {
  try {
    let result = await db.query(`select detail_name  from package_cycle as t1 inner join release_package_cycle as t2 on (t1.id=t2.cycle_id) inner join package_detail as t3 on(t2.detail_id=t3.id) where t1.cycle_name like '${series}%'`)
    if (result.length < 1) {
      throw new Error('系列不存在,查询失败')
    }
    result = result.map((item) => {
      return item.detail_name
    })
    return result
  } catch (error) {
    throw error
  }
}

module.exports = {
  queryPackBySeries, querySeries, queryPackage, queryType, queryRare
}